var express = require('express');
var router = express.Router();

router.all('/:id(\\d+)/?*', function(req, res, next) {
    var id = parseInt(req.params.id);
    if(!isNaN(id)) {
        req.model.i_id = id;
        req.pool.acquire(function(err, conn) {
        	conn.call('issues.getIssueInfo', {i_id: req.model.i_id}, function(err, result) {
        		req.pool.release(conn);
        		req.model.data = result;
        		next();
        	});
        });
    } else {
        req.model.warningAlerts.push('wrong id');
        req.session.model = req.model;
        res.redirect("/Periodicals/");
    }
});

router.post('/:id(\\d+)/update', function(req, res) {
    var params = {};
    params.i_id = req.model.i_id;
    params.i_num = req.body.num;
    params.i_r_date = req.body.release_date;

    console.log(params);

    req.pool.acquire(function(err, conn) {
        conn.call('issues.updateIssue', params, function(err, result) {
            req.pool.release(conn);

            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Succesfully updated');
            }

            req.session.model = req.model;
            res.redirect('/Issues/' + params.i_id);
        });
    });
});

router.get('/:id(\\d+)/article/:a_id(\\d+)/delete', function(req, res) {
    var a_id = parseInt(req.params.a_id);
    req.pool.acquire(function(err, conn) {
        conn.call('articles.deleteArticle', {a_id: a_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Successfully deleted');
            }
            req.session.model = req.model;
            res.redirect('/Issues/' + req.model.i_id );
        });
    });
});

router.post('/:id(\\d+)/article/create', function(req, res) {
    var params = {};
    params.i_id = req.model.i_id;
    params.s_title = req.body.s_title;

    req.pool.acquire(function(err, conn) {
        conn.call('articles.createArticle', params, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.dangerAlerts.push(result.error);
            } else {
                req.model.successAlerts.push('Successfully created');
            }
            req.session.model = req.model;
            res.redirect('/Issues/' + req.model.i_id);
        });
    });
});

router.get('/:id(\\d+)/?*', function(req, res, next) {
    req.pool.acquire(function (err, conn) {
        conn.call('articles.getArticlesPagesCount', {i_id: req.model.i_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.warningAlerts.push(result.error);
                req.model.pages = 0;
            } else {
                req.model.pages = result.count;
            }
            next();
        });
    });
});

router.get('/:id(\\d+)/?*', function(req, res, next) {
    req.model.page = 0;
    req.pool.acquire(function (err, conn) {
        conn.call('articles.getArticles', {i_id: req.model.i_id}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.warningAlerts.push(result.error);
                req.model.articles = [];
            } else {
                req.model.articles = result;
            }
            next();
        });
    });
});

router.get('/:id(\\d+)/page:pg(\\d+)', function(req, res, next) {
    req.pool.acquire(function (err, conn) {
        req.model.page = parseInt(req.params.pg);
        if(isNaN(req.model.page)) req.model.page = 0;
        conn.call('articles.getArticles', {i_id: req.model.i_id, page: req.model.page}, function(err, result) {
            req.pool.release(conn);
            if(result.error) {
                req.model.warningAlerts.push(result.error);
                req.model.issues = [];
            } else {
                req.model.issues = result;
            }
            next();
        });
    });
});

router.get('/:id(\\d+)/?*', function(req, res) {
    req.pool.acquire(function (err, conn) {
        res.render('issue', req.model);
    });
});

module.exports = router;